#include "updateinfo.h"
#include <iostream>
#include <QDebug>

using namespace std;

UpdateInfo::UpdateInfo(QObject *parent) : QObject(parent)
{
    _fileUpdate = new QFile("updata.xml");

    version = QString("0");
    port = 3327;
    ip = QString("192.168.0.70");
    ReadInfoFromXml();
}

UpdateInfo::~UpdateInfo()
{
    delete _fileUpdate;
    fileNameList.clear();
}

bool UpdateInfo::SaveInfoToXml(const QString &fileName)
{
    _fileUpdate->setFileName(fileName);
    if (!_fileUpdate->open(QFile::WriteOnly | QFile::Text))
    {
        std::cerr << "err:can't open:" <<qPrintable(_fileUpdate->fileName())
                  << ":"<<qPrintable(_fileUpdate->errorString())
                  << std::endl;
        _fileUpdate->close();
        return false;
    }
    xmlWriter.setDevice(_fileUpdate);
    xmlWriter.setAutoFormatting(true);
    xmlWriter.setAutoFormattingIndent(8);

    xmlWriter.writeStartDocument();
    xmlWriter.writeDTD("<!DOCTYPE updateInfo>");
    xmlWriter.writeStartElement("status");
    xmlWriter.writeAttribute("version", "1.0");

    xmlWriter.writeStartElement(QString("network"));
    xmlWriter.writeTextElement("ip", ip);
    xmlWriter.writeTextElement("port", QString::number(port));
    xmlWriter.writeEndElement();

    xmlWriter.writeStartElement(QString("update"));
    xmlWriter.writeTextElement("version",version);
    foreach (QString name, fileNameList) {
        xmlWriter.writeTextElement("file", name);
    }
    xmlWriter.writeTextElement("updateDate",updateDate);
    xmlWriter.writeEndElement();

    xmlWriter.writeEndDocument();
    _fileUpdate->close();
    return true;
}

bool UpdateInfo::ReadInfoFromXml(const QString &fileName)
{
    fileNameList.clear();
    _fileUpdate->setFileName(fileName);
    if (!_fileUpdate->open(QFile::ReadOnly | QFile::Text))
    {
        cerr << "err:can't open:" <<qPrintable(_fileUpdate->fileName())
             << ":"<<qPrintable(_fileUpdate->errorString())
             << endl;
        _fileUpdate->close();
        return false;
    }
    xmlReader.setDevice(_fileUpdate);

    if (xmlReader.readNextStartElement()) {
        if (xmlReader.name() == "status" && xmlReader.attributes().value("version") == "1.0"){
            while (xmlReader.readNextStartElement()) {
                if (xmlReader.name() == "network"){
                    readNetwork();
                }
                else if (xmlReader.name() == "update"){
                    readFileInfo();
                }
            }
        }else{
            xmlReader.raiseError(QObject::tr("The file is not an XBEL version 1.0 file."));
        }
    }
    _fileUpdate->close();
    print();
    return true;
}

//读取网络配置信息
void UpdateInfo::readNetwork()
{
    Q_ASSERT(xmlReader.isStartElement() && xmlReader.name() == "network");
    QString temp;
    while (xmlReader.readNextStartElement()) {
        if(xmlReader.name() == "ip"){
            temp = xmlReader.readElementText();
            if(checkData(temp))
                ip = temp;
        }
        else if(xmlReader.name() == "port"){
            temp = xmlReader.readElementText();
            if(checkData(temp) && temp.toInt() > 0)
                port = temp.toInt();
        }

    }
}

//读取更新文件的信息
void UpdateInfo::readFileInfo()
{
    Q_ASSERT(xmlReader.isStartElement() && xmlReader.name() == "update");
    QString temp;
    while (xmlReader.readNextStartElement()) {
        if(xmlReader.name() == "version"){
            temp = xmlReader.readElementText();
            if(checkData(temp))
                version = temp;
        }else if(xmlReader.name() == "file"){
            temp = xmlReader.readElementText();
            if(checkData(temp))
                fileNameList.append(temp);
        }else if(xmlReader.name() == "updateDate"){
            temp = xmlReader.readElementText();
            if(checkData(temp))
                updateDate = temp;
        }
    }
}

bool UpdateInfo::checkData(const QString &data)
{
    if(!data.isEmpty() && !data.isNull()){
        return true;
    }else{
        return false;
    }
}

//打印成员变量
void UpdateInfo::print()
{
    cout << "======================= data ==================="<<endl;
    cout << "version:" << qPrintable(version) << endl;
    cout << "host ip:"<< qPrintable(ip) << endl;
    cout << "port:" << port << endl;

    int filecount = 0;
    foreach (QString filename, fileNameList) {
        cout << "file" << ++filecount << ":" << qPrintable(filename) << endl;
    }
    cout << "======================= data ==================="<<endl;
}

void UpdateInfo::setPort(const int &port)
{
    this->port = port;
}

int UpdateInfo::getPort()
{
    return port;
}

void UpdateInfo::setIp(const QString &ip)
{
    this->ip = ip;
}

QString UpdateInfo::getIp()
{
    return ip;
}

void UpdateInfo::setVersion(const QString &version)
{
    this->version = version;
}

QString UpdateInfo::getVersion()
{
    return version;
}

void UpdateInfo::setUpdateDate(const QString &date)
{
    this->updateDate = date;
}

QString UpdateInfo::getUpdateDate()
{
    return updateDate;
}
